package com.jsy.order.listener;


import com.alibaba.fastjson.JSON;
import com.jsy.common.util.IdWorker;
import com.jsy.order.pojo.OrderLog;
import com.jsy.order.service.OrderLogService;
import com.jsy.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@RabbitListener(queues = "${mq.pay.queue.order}")
public class PayMessageListener {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private IdWorker idWorker;

    @RabbitHandler
    public void reiveMsg(String msg){
        Map<String,String> map = JSON.parseObject(msg, Map.class);
        String orderId = map.get("out_trade_no");
        if ("SUCCESS".equals(map.get("result_code"))){
            orderService.updateStatus(orderId,"1",map.get("transaction_id"));

            OrderLog orderLog = new OrderLog();
            orderLog.setId(idWorker.nextId()+"");
            orderLog.setOrderId(orderId);
            orderLog.setOrderStatus("0");
            orderLog.setPayStatus("1");
            orderLog.setRemarks("创建支付记录");
            orderLog.setMoney(Integer.parseInt(map.get("total_fee")));
            orderLogService.add(orderLog);
        }else {
            orderService.updateStatus(orderId,"2",null);
        }
    }
}
